package com.example.geoquiz

import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.example.myapplicationandroidstudy.R
import com.example.util.Mlog

private const val TAGGQMA = "GeoQuizMainActivity"
private const val KEY_INDEX = "index"

class GeoQuizMainActivity : AppCompatActivity() {

    private val quizViewModel:QuizViewModel by lazy {
        ViewModelProviders.of(this).get(QuizViewModel::class.java)
    }
    fun logi(str: String?) {
        Log.i(TAGGQMA, str!!)
    }

    private lateinit var trueButton:Button;
    private lateinit var falseButton:Button;
    private lateinit var nextButton:Button;
    private lateinit var pervButton:Button;
    private lateinit var questionTextView:TextView;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Mlog.i("onCreate called")
        enableEdgeToEdge()
        setContentView(R.layout.activity_geo_quiz_main)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
        quizViewModel.currentIndex=savedInstanceState?.getInt(KEY_INDEX,0)?:0
        trueButton=findViewById(R.id.true_button);
        falseButton=findViewById(R.id.false_button);
        nextButton=findViewById(R.id.next_button);
        pervButton=findViewById(R.id.perv_button);
        questionTextView=findViewById(R.id.question_text_view);

        trueButton.setOnClickListener { view:View ->
            Mlog.i("click trueButton");
            checkAnswer(true)
         }

        falseButton.setOnClickListener { view:View ->
            Mlog.i("click falseButton");
            checkAnswer(false)
        }
        nextButton.setOnClickListener {
            quizViewModel.moveToNext()
            updateQuestion()
        }
        pervButton.setOnClickListener {
            quizViewModel.moveToPrev()
            updateQuestion()
        }

        updateQuestion()
    }

    override fun onStart() {
        super.onStart()
        Mlog.i("onStart called")

    }

    override fun onResume() {
        super.onResume()
        Mlog.i("onResume called")

    }

    override fun onPause() {
        super.onPause()
        Mlog.i("onPause called")

    }

    /**
     * 保存永久性的数据
     */
    override fun onStop() {
        super.onStop()
        Mlog.i("onStop called")

    }

    override fun onDestroy() {
        super.onDestroy()
        Mlog.i("onDestroy called")

    }

    private fun updateQuestion() {
        val questionTextResId = quizViewModel.currentQuestionText
        questionTextView.setText(questionTextResId)
    }

    private fun checkAnswer(userAnswer:Boolean){
        val correctAnswer = quizViewModel.currentQuestionAnswer
        val messageResId=if(userAnswer==correctAnswer){
            R.string.correct_toast
        } else {
            R.string.incorrect_toast
        }
        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT)
                .show()
    }

    /**
     * 暂存临时性的数据
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        logi("onSaveInstanceState called")
        outState.putInt(KEY_INDEX,quizViewModel.currentIndex)
    }
}